//
//  Profile.m
//  GradeKeeper
//
//  Created by Lily Wang on 11/1/09.
//  Copyright 2009 i5. All rights reserved.
//

#import "Profile.h"
/*

@implementation Profile
@synthesize userName, enteredCGPA, enteredCreditsUsed, enteredSemester;
static Profile *sharedProfile = nil;

//Creates a global profile that can be shared between ui view change
//This global profile will contain information on the currently selected profile
+ (Profile *) sharedProfile{
    @synchronized(self) {
        if (sharedProfile == nil) {
            sharedProfile = [[Profile alloc] init];
			NSLog(@"profile alloc");
        }
    }
    return sharedProfile;
} //sharedProfile

- (void) setUserName: (NSString*) inputName {
	userName = inputName;
} //setUserName

- (NSString*) getUserName {
	return userName;
	
} //getUserName

- (void) setCGPA: (NSString*) cgpa {
	enteredCGPA = cgpa;
	NSLog(@"EnteredCGPA called: %@, %@", enteredCGPA, userName);
} //setCGPA


-(NSString*) getCGPA {
	NSLog(@"get cgpa called %@", enteredCGPA);
	return enteredCGPA;
	
} //getCGPA

- (void) setCreditsUsed: (NSString*) creditsUsed {
	enteredCreditsUsed = creditsUsed;
} //setCreditsUsed


-(NSString*) getCreditsUsed {
	return enteredCreditsUsed;
} //getCreditsUsed

- (void) setSemester: (NSString*) semester {
	enteredSemester = semester;
	NSLog(@"EnteredSemester called: %@", enteredSemester );
} //setSemester


-(NSString*) getSemester {
	NSLog(@"getSemester called: %@", enteredSemester );
	return enteredSemester;
} //getSemester


- (void)dealloc {
	[enteredCGPA release];
	[enteredCreditsUsed release];
	[enteredSemester release];
    [super dealloc];
}


@end
*/

@implementation Profile
@synthesize userName, enteredCGPA, enteredCreditsUsed, enteredSemester, takenCredits, semesters;
static Profile *sharedProfile = nil;

//Creates a global profile that can be shared between ui view change
//This global profile will contain information on the currently selected profile
+ (Profile *) sharedProfile{
	if(sharedProfile) return sharedProfile;
	
    @synchronized(self) {
        if (!sharedProfile) {
            sharedProfile = [[self alloc] init];
        }
		return sharedProfile;
    }
    return nil;
} //sharedProfile

- (void) setUserName: (NSString*) inputName {
	userName = inputName;
} //setUserName

- (NSString*) getUserName {
	return userName;
	
} //getUserName

- (void) setCGPA: (NSString*) cgpa {
	enteredCGPA = cgpa;
	NSLog(@"EnteredCGPA called: %@, %@", enteredCGPA, userName);
} //setCGPA


-(NSString*) getCGPA {
	NSLog(@"get cgpa called %@", enteredCGPA);
	return enteredCGPA;
	
} //getCGPA

- (void) setCreditsUsed: (NSString*) creditsUsed {
	enteredCreditsUsed = creditsUsed;
} //setCreditsUsed


-(NSString*) getCreditsUsed {
	return enteredCreditsUsed;
} //getCreditsUsed

- (void) setSemester: (NSString*) semester {
	enteredSemester = semester;
	NSLog(@"EnteredSemester called: %@", enteredSemester );
} //setSemester


-(NSString*) getSemester {
	NSLog(@"getSemester called: %@", enteredSemester );
	return enteredSemester;
} //getSemester

-(BOOL) addSemester:(Semester*) newSemester {
	NSEnumerator *enumerator = [self.semesters objectEnumerator];
    id obj;
	NSUInteger i = 0;
	
    while ( obj = [enumerator nextObject] ) {
        if ([obj year] > [newSemester year]) {
			if([[obj term] compare: [newSemester term]] == NSOrderedDescending){
				[self.semesters insertObject:newSemester atIndex:i];
				return YES;
			} else if ([[obj term] compare: [newSemester term]] == NSOrderedSame) {
				return NO;
			}
		}
		i++;
    }
	
	[self.semesters addObject:newSemester];
	return YES;
}

-(void) update {
	int newCredits = 0;
	float sumGPA = 0;
	
    for (int i=0; i<[self.semesters count]; i++) {
		newCredits += [[self.semesters objectAtIndex:i] credits];
		sumGPA += [[self.semesters objectAtIndex:i] GPA]*[[self.semesters objectAtIndex:i] credits];
	}
	
	self.takenCredits = [NSString stringWithFormat:@"%i", newCredits];
	self.enteredCGPA = [NSString stringWithFormat:@"%f", sumGPA/newCredits];
}
@end

